package com.ruoyi.elasticsearch.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.elasticsearch.contant.StatusCode;
import com.ruoyi.elasticsearch.domain.PageResult;
import com.ruoyi.elasticsearch.domain.RequestParams;
import com.ruoyi.elasticsearch.domain.Result;
import com.ruoyi.elasticsearch.es.service.HotelEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.elasticsearch.domain.Hotel;
import com.ruoyi.elasticsearch.service.IHotelService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 酒店列表Controller
 *
 * @author 林道敬
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/hotel")
public class HotelController extends BaseController
{
    @Autowired
    private IHotelService hotelService;
    @Autowired
    HotelEsService hotelEsService;

    // 搜索酒店数据
    @PostMapping("/searchList")
    public PageResult search(@RequestBody RequestParams params){
        System.out.println("params :" + params);
        return hotelEsService.search(params);
    }

    @GetMapping("getBrandName")
    public Set<String> getBrandName(){
        List<Hotel> list = hotelService.list();
        Set<String> brandnames = list.stream().map(Hotel::getBrand).collect(Collectors.toSet());
        return brandnames;
    }

    @GetMapping("getStarName")
    public Set<String> getStarName(){
        List<Hotel> list = hotelService.list();
        Set<String> starNames = list.stream().map(Hotel::getStarName).collect(Collectors.toSet());
        return starNames;
    }

    @GetMapping(value = "/all/add")
    public Result addAll(){
        try {
            //添加数据到索引库中
            hotelEsService.addAll();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return new Result(true, StatusCode.OK,"导入所有数据到索引库成功！");
    }

    @PostMapping("filters")
    public Map<String, List<String>> getFilters(@RequestBody RequestParams params) {
        return hotelEsService.getFilters(params);
    }
    /**
     * 查询酒店列表列表
     */
    @RequiresPermissions("elasticsearch:hotel:list")
    @GetMapping("/list")
    public TableDataInfo list(Hotel hotel)
    {
        startPage();
        List<Hotel> list = hotelService.selectHotelList(hotel);
        return getDataTable(list);
    }

    /**
     * 导出酒店列表列表
     */
    @RequiresPermissions("elasticsearch:hotel:export")
    @Log(title = "酒店列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Hotel hotel)
    {
        List<Hotel> list = hotelService.selectHotelList(hotel);
        ExcelUtil<Hotel> util = new ExcelUtil<Hotel>(Hotel.class);
        util.exportExcel(response, list, "酒店列表数据");
    }

    /**
     * 获取酒店列表详细信息
     */
    @RequiresPermissions("elasticsearch:hotel:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hotelService.selectHotelById(id));
    }

    /**
     * 新增酒店列表
     */
    @RequiresPermissions("elasticsearch:hotel:add")
    @Log(title = "酒店列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Hotel hotel)
    {
        return toAjax(hotelService.insertHotel(hotel));
    }

    /**
     * 修改酒店列表
     */
    @RequiresPermissions("elasticsearch:hotel:edit")
    @Log(title = "酒店列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Hotel hotel)
    {
        return toAjax(hotelService.updateHotel(hotel));
    }

    /**
     * 删除酒店列表
     */
    @RequiresPermissions("elasticsearch:hotel:remove")
    @Log(title = "酒店列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hotelService.deleteHotelByIds(ids));
    }
}
